package com.after.saas.datasource.handler;

import com.after.saas.common.model.vo.UserVO;
import com.after.saas.common.utils.UserUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Classname MyMetaObjectHandler
 * @Description TODO
 * @Date 2021/6/15 8:45
 * @Created by hjchen
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private UserUtil userUtil;

    @Override
    public void insertFill(MetaObject metaObject) {
        log.warn("start insert fill ....");
        UserVO userVO = userUtil.getUserInfo();
        this.strictInsertFill(metaObject, "createBy", userVO::getId, String.class);
        this.strictUpdateFill(metaObject, "updateBy", userVO::getId, String.class);
        this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.warn("start update fill ....");
        UserVO userVO = userUtil.getUserInfo();
        this.strictUpdateFill(metaObject, "updateBy", userVO::getId, String.class);
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
    }
}